package com.shuqi.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.shuqi.activity.viewport.CommonTitle;
import com.shuqi.application.ShuqiApplication;
import com.shuqi.common.Config;
import com.shuqi.common.ConfigVersion;
import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.SharedPreferenceUtil;
import com.shuqi.common.utils.StringUtils;
import com.shuqi.common.utils.Util;
import com.shuqi.controller.R;
import com.shuqi.interfaces.EventTypeConstants;
import com.shuqi.interfaces.IEventListener;
import com.shuqi.model.bean.AppUpdateInfo;
import com.shuqi.service.UpdateService;

/**
 * 关于书旗界面
 * 
 * @author linyuren
 * 
 */
public class AboutShuqiActivity extends ActivityBase implements OnClickListener, IEventListener {

    private final static String TAG = "AboutShuqiActivity";

    private final static int init = EventTypeConstants.BASE;

    private final static int initDataHotSuccess = init + 1;

    private Intent intent;

    private TextView tvCurrentAppDesc;

    private AppUpdateInfo info;

    private CommonTitle mTitle;
    private TextView mVersionTextView;
    private String mVersion;
    private TextView mDitchTextView;
    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case init:
                    activityLogicForward();
                    break;
                case initDataHotSuccess:
                    activityInitData();
                    break;
                default:
                    break;
            }
        };
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.act_about_shuqi_view);
        intent = getIntent();
        info = (AppUpdateInfo) intent.getSerializableExtra("AppUpdateInfo");
        tvCurrentAppDesc = (TextView) findViewById(R.id.current_app_desc);
        if (info != null && StringUtils.isNotBlank(info.getIntro2())) {
            int oldVersion = Integer.valueOf(Config.VERSION_INFO);
            int newVersion = Integer.valueOf(info.getVersion2());
            if (oldVersion < newVersion) {
                tvCurrentAppDesc.setText("有新版本，请点击更新");
                findViewById(R.id.check_update).setOnClickListener(this);
            }
        }

        mVersion = Util.getAppVersionName(AboutShuqiActivity.this);

        handler.sendEmptyMessage(init);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left:
                ActivityManager.getInstance().finishActivity(this);
                break;
            case R.id.check_update:
                UpdateService.startDownLoadAppService(ShuqiApplication.getContext(), info.getUpdate_url2());
                SharedPreferenceUtil.setSoftUpdate(this, false);//不显示软件更新红点
                break;
            // 进入功能介绍
            case R.id.function_introduce:
                NewbieGuideActivity.show(this, true);
                break;
            // 给好评
            case R.id.high_praise:
                try {
                    Uri localUri = Uri.parse("market://details?id=" + getPackageName());
                    startActivity(new Intent(Intent.ACTION_VIEW, localUri));
                } catch (Exception e) {
                    Toast.makeText(AboutShuqiActivity.this,
                            getResources().getString(R.string.no_app_market), Toast.LENGTH_SHORT)
                            .show();
                    Log4an.i(TAG,"在给个好评中："+e);
                    e.printStackTrace();
                }
                break;
        }
    }

    @Override
    public void onEvent(int event, Object data) {
        Log4an.d(TAG, "event:" + event + ",result:" + data);
        switch (event) {

            default:
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void activityLogicForward() {
        mTitle = (CommonTitle) findViewById(R.id.title);
        mTitle.setLeftClickListener(this);
        mVersionTextView = (TextView) findViewById(R.id.app_version);
        mDitchTextView = (TextView) findViewById(R.id.app_ditch);
        this.findViewById(R.id.function_introduce).setOnClickListener(this);
        this.findViewById(R.id.high_praise).setOnClickListener(this);
        mVersionTextView.setText(getResources().getString(R.string.app_version_toast) + " "
                + mVersion);
        String fr_or_klink = "";
        if (ConfigVersion.FR != null && !"".equals(ConfigVersion.FR)
                && !"xxxx".equals(ConfigVersion.FR)) {
            fr_or_klink = ConfigVersion.FR;
        } else if (ConfigVersion.KLINK != null && !"".equals(ConfigVersion.KLINK)
                && !"xxxx".equals(ConfigVersion.KLINK)) {
            fr_or_klink = ConfigVersion.KLINK;
        }
        String firstPlaceid = "";
        if (!TextUtils.isEmpty(ConfigVersion.FIRST_PLACEID)) {
            firstPlaceid = " " + ConfigVersion.FIRST_PLACEID;
        }
        mDitchTextView.setText(getResources().getString(R.string.app_bottom_version_toast) + "  "
                + Config.VERSION_INFO + " " + ConfigVersion.ANSITEID + firstPlaceid + fr_or_klink);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            ActivityManager.getInstance().finishActivity(this);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void activityInitData() {

    }
}
